<template>
  <div class="bj">
    <div class="left">
      <div class="title">
        <p>ORDER聚造</p>
      </div>
      <el-menu
        default-active="2"
        class="el-menu-vertical-demo"
        @open="handleOpen"
        @close="handleClose"
        background-color="transparent"
        text-color="#fff"
        :router="true"
        active-text-color="#ffd04b"
      >
        <el-submenu index="1">
          <template slot="title">
            <i class="el-icon-s-home"></i>
            <span>首页管理</span>
          </template>
          <el-menu-item-group>
            <el-menu-item index="/index/banners">轮播图管理</el-menu-item>
            <el-menu-item index="/index/category">分类区管理</el-menu-item>
            <el-menu-item index="/index/recomend">为你推荐管理</el-menu-item>
          </el-menu-item-group>
        </el-submenu>

        <el-submenu index="2">
          <template slot="title">
            <i class="el-icon-location"></i>
            <span>商品管理</span>
          </template>
          <el-menu-item-group>
            <el-menu-item index="/index/fenlei">分类管理</el-menu-item>
            <el-menu-item index="/index/brand">品牌管理</el-menu-item>
            <el-menu-item index="/index/goods">商品管理</el-menu-item>
            <el-menu-item index="/index/one">单品管理</el-menu-item>
          </el-menu-item-group>
        </el-submenu>

        <el-submenu index="3">
          <template slot="title">
            <i class="el-icon-s-tools"></i>
            <span>新闻管理</span>
          </template>
          <el-menu-item-group>
            <el-menu-item index="/index/news">新闻管理</el-menu-item>
          </el-menu-item-group>
        </el-submenu>
      </el-menu>
    </div>
    <div class="right">
      <div style="display: flex; justify-content: space-between">
        <div style="padding-top: 45px">
          <b>{{ $route.meta.title }}</b>
        </div>
        <div class="nick">
          <img src="../assets/indeximage/nick.png" alt="4" />
          <el-tooltip>
            <div slot="content" @click="dialogFormVisible = true">修改密码</div>
            <div class="name">{{ nickName }},您好!</div>
          </el-tooltip>
          <div class="esc" @click="logout">退出</div>
        </div>
      </div>
      <router-view> </router-view>
    </div>
    <!-- 修改密码 -->
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible" width="30%">
      <el-form
        :model="form"
        status-icon
        :rules="rules"
        ref="form"
        label-width="100px"
      >
        <el-form-item label="原密码" prop="passWord">
          <el-input type="password" v-model="form.passWord"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassWord">
          <el-input type="password" v-model="form.newPassWord"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="reNewPassWord">
          <el-input type="password" v-model="form.reNewPassWord"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="changePassWord">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
     <script>
import md5 from "js-md5";
export default {
  name: "index",
  components: {},
  data() {
    return {
      photo: "",
      nickName: "",
      dialogFormVisible: false,
      form: {
        passWord: "",
        newPassWord: "",
        reNewPassWord: "",
      },
      group: {
        //选中的群组
        groupId: "",
        groupName: "",
      },
      menus: [],
      rules: {
        passWord: [
          { required: true, message: "请输入原密码", trigger: "blur" },
        ],
        newPassWord: [
          { required: true, message: "请输入新密码", trigger: "blur" },
        ],
        reNewPassWord: [
          { required: true, message: "请重复输入新密码", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(new Error("请再次输入密码"));
              } else if (value !== this.form.newPassWord) {
                callback(new Error("两次输入密码不一致!"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    changePassWord() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$http({
            url: "/admin/changePassWord",
            method: "POST",
            data: {
              passWord: md5(this.form.passWord),
              newPassWord: md5(this.form.newPassWord),
              //   userName:this.form.userName
            },
          }).then((res) => {
            if (!res.errorMsg) {
              this.$message({
                message: "修改密码成功",
                type: "success",
                duration: 3000,
              });
              setTimeout(() => {
                location.href = "#/login";
              }, 3000);
            }
          });
        }
      });
    },
    logout() {
      this.$confirm("是否退出登录?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http({
          url: "/admin/logout",
        }).then((res) => {
          if (res.errorMsg) {
            this.$message.error(res.errorMsg);
          } else {
            localStorage.removeItem("token");
            localStorage.removeItem("user");
            location.href = "#/login";
          }
        });
      });
    },
  },
  created() {
    //从缓存获取登录用户信息
    let user = localStorage.getItem("user");
    if (!user) {
      //   this.$router.push('/login')
      //   history.reload()
      console.log(history, location);
      location.href = location.origin + "#/login";
    }
    //解析为对象
    user = JSON.parse(user);
    //设置头像
    this.photo = user.avatarUrl || require("../assets/indeximage/nick.png");
    //设置昵称
    this.nickName = user.nickName || "尊敬的用户";
    //获取菜单
    //this.getMenus()
  },
  mounted() {},
};
</script>
    <style scoped>
.bg {
  background: #eff6fc;
  width: 1440px;
  height: 768px;
  margin-left: 110px;
  background-size: 100% 100%;
  background-attachment: local;
  margin-top: -15px;
}
.left {
  background-image: url("../assets/indeximage/leftbg.png");
  width: 210px;
  height: 768px;
  background-size: cover;
  position: absolute;
}

.title {
  width: 100px;
  height: 23px;
  font-size: 16px;
  font-family: MicrosoftYaHeiUI-Bold-, MicrosoftYaHeiUI-Bold;
  color: #ffffff;
  font-weight: bold;
  margin-left: 47px;
}
.right {
  margin-left: 298px;
}

.rightbox {
  width: 1124px;
  height: 618px;
  background: cornflowerblue;
  border-radius: 0px 0px 0px 0px;
  opacity: 1;
  display: flex;
}
.nick {
  display: flex;
  align-items: center;
  padding-top: 10px;
  margin-right: 24px;
  /* width: 100%; */
}
.name {
  color: #eab924;
  margin: 0px 10px;
}
.esc {
  width: 85px;
  height: 25px;
  padding-top: 4px;
  margin-left: 10px;
  border-radius: 30px 30px 30px 30px;
  background: red;
  color: #fff;
  text-align: center;
}
</style >
  
